package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockIn;
import com.fowo.api.entity.StockInProduct;
import com.fowo.api.inventory.model.InventoryStorageVo;
import com.fowo.api.inventory.model.SimpleInventoryItem;
import com.fowo.api.inventory.model.TargetTypeEnum;
import com.fowo.api.inventory.model.form.InventoryStorageFormVo;
import com.fowo.api.inventory.service.InventoryBusinessService;
import com.fowo.api.model.stock.in.StockInApplyInStorageAction;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.service.StockInProductService;
import com.fowo.api.service.impl.StockInProductServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StockInApplyInStorage implements StockInApplyInStorageAction {

    @Resource
    private StockInServiceImpl stockInService;
    @Resource
    protected StockInProductServiceImpl stockInProductService;
    @Resource
    private InventoryBusinessService inventoryBusinessService;
    @Override
    public void run(Long id) throws Exception {
        StockInVo byId = stockInService.getVoById(id);
        Assert.notNull(byId,"没有找到这个入库单");
        Assert.isTrue(byId.getStatus().equals("0"),"违规操作!");
        List<StockInProduct> list = stockInProductService.list(Wrappers.<StockInProduct>query().lambda().eq(StockInProduct::getParentStockInId, id));
        Assert.isTrue(byId.getStatus().equals("0"),"违规操作!");
        List<StockInProduct> collect = list.stream().filter(x -> x.getUnitPrice() == null).collect(Collectors.toList());
        Assert.isTrue(collect.size()==0,"请设置产品单价！");
        List<StockInProduct> collect1 = list.stream().filter(x -> x.getQuantity() == null).collect(Collectors.toList());
        Assert.isTrue(collect1.size()==0,"请设置入库数量！");
        InventoryStorageFormVo formVo = prepareApplyInStorage(byId);
        inventoryBusinessService.storage(formVo);
        // 更新成功状态
        StockIn updateStatusEntity = new StockIn();
        updateStatusEntity.setId(id);
        updateStatusEntity.setStatus("1");
        stockInService.updateById(updateStatusEntity);
    }
    /**
     * 准备用于 审核 的参数
     */
    protected InventoryStorageFormVo prepareApplyInStorage(StockInVo vo)
            throws Exception {
        if (!"0".equals(vo.getStatus())) {
            throw new Exception("当前单据状态不可进行此操作！");
        }
        InventoryStorageFormVo formVo = new InventoryStorageFormVo();
        formVo.setTo(TargetTypeEnum.Normal);
        formVo.setFormNum(vo.getOrderSn());
        formVo.setFormOperator(vo.getCreateUser().toString());
        formVo.setStockId(vo.getWarehouse());
        for (StockInProductItemVo stockInProduct : vo.getProductList()) {
            SimpleInventoryItem inventoryItem = new SimpleInventoryItem();
            inventoryItem.setId(stockInProduct.getProductId());
            InventoryStorageVo storageItem = new InventoryStorageVo();
            storageItem.setItem(inventoryItem);
            storageItem.setQty(BigDecimal.valueOf(stockInProduct.getQuantity()));
            storageItem.setAmount(stockInProduct.getProductPrice());
            formVo.add(storageItem);
        }
        return formVo;
    }
}
